package com.jiangds.open.check.spring.aop;

import com.jiangds.open.annotation.NeedCheck;
import com.jiangds.open.check.Check;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;


/**
 * {@link CheckHandler}
 *
 * @author jiangliang
 * @since 2021/1/8 14:11
 */
public class CheckHandler implements MethodBeforeAdvice {

    private Check check;

    public CheckHandler(Check check) {
        this.check = check;
    }

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        Annotation[][] pls = method.getParameterAnnotations();
        for (int i = 0; i < pls.length; i++) {
            Annotation[] pl = pls[i];
            for (Annotation annotation : pl) {
                if(annotation.annotationType() == NeedCheck.class){
                    check.checkObject(args[i]);
                    break;
                }
            }
        }
    }
}
